﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RDAExplorer;

namespace Manager2070.Library
{
    public class Patcher
    {

        frmPatchApply _form = null;
        string _destinationRda = null;
        string _restoreRda = null;
        string _tempFolder = null;


        public void ShowPatchApplyScreen()
        {
            DisposeForm();

            _form = new frmPatchApply(this);
            _form.Show();
        }

        public void GameNotReady(string msg)
        {
            if (_form != null)
            {
                _form.DisableScreen();
                _form.SetStatus(msg);
            }
        }

        public void Quit()
        {
            DisposeForm();
        }

        private void DisposeForm()
        {
            if (_form != null)
            {
                _form.Close();
                _form = null;
            }
        }

        public void ApplyPatch(string sourceFile)
        {
            Game g = new Game();
            _destinationRda = g.GetNewestPatchPath();
            _restoreRda = _destinationRda + ".2070";

            //todo
            // create temp folder
            _form.SetStatus("Creating work folder...");

            // extract latest rda to temp folder
            _form.SetStatus("Extracting files from original RDA...");
            RDAReader rdaReader = new RDAExplorer.RDAReader(_destinationRda);
            UISettings.EnableConsole = false;
            rdaReader.rdaFolder.GetAllFiles().ExtractAll(@"c:\\temp");
            //System.Threading.Thread.Sleep(5000);

            // extract source file to temp folder and overwrite
            _form.SetStatus("Extracting files from Mod...");
            //http://dotnetzip.codeplex.com/

            // recreate the rda file
            _form.SetStatus("Building modded RDA...");

            // copy patchx.rda to patchx.rda.2070
            _form.SetStatus("Backing up original RDA...");

            // move created rda file to \maindata folder
            _form.SetStatus("Deploying modded RDA...");

            _form.SetStatus("Patch Applied!");

        }
    }
}
